package com.zxtyshangjia.zxtyjms.business.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxtyshangjia.zxtyjms.R;
import com.zxtyshangjia.zxtyjms.app.Myappalication;
import com.zxtyshangjia.zxtyjms.business.bean.ShangjiaList;
import com.zxtyshangjia.zxtyjms.utils.Api;

import java.util.List;

/**
 * Created by 18222 on 2017/11/16.
 */

public class ShangjiaAdapter extends BaseAdapter {


    private Context ctx;
    private List<ShangjiaList> list;
    private LayoutInflater inflater;


    public ShangjiaAdapter(Context ctx,List<ShangjiaList> list) {
        this.ctx = ctx;
        this.list = list;
        inflater = LayoutInflater.from(ctx);
    }

    @Override
    public int getCount() {
        return list!=null? list.size():0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if(view == null){
            view = inflater.inflate(R.layout.sj_list_item,null);
            holder = new ViewHolder();
            holder.mImg = (ImageView) view.findViewById(R.id.sj_headimg_iv);
            holder.mName = (TextView) view.findViewById(R.id.sj_sjname_tv);
            holder.mTime = (TextView) view.findViewById(R.id.sj_sjtime_tv);
            holder.mAddress = (TextView) view.findViewById(R.id.sj_address_tv);
            holder.mTelPhone = (TextView) view.findViewById(R.id.sj_telphone_tv);
            holder.mStatusLL = (LinearLayout) view.findViewById(R.id.sj_status_ll);
            holder.mStatusTV = (TextView) view.findViewById(R.id.sj_status_tv);

            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        ShangjiaList item = list.get(i);


        if(item.head_pic.indexOf("https://") != -1 || item.head_pic.indexOf("http://") != -1)
        {
            Myappalication.getGlideManager().inputImage(item.head_pic, holder.mImg);
        }else {
            Myappalication.getGlideManager().inputImage(Api.PIC_URL + item.head_pic, holder.mImg);
        }

        holder.mName.setText(item.name);
        holder.mTime.setText(item.ctime);
        holder.mAddress.setText(item.address);
        holder.mTelPhone.setText(item.account);

        if(item.status.equals("0")){
            //商家状态，0正常，1待审核，2审核失败
            holder.mStatusLL.setVisibility(View.GONE);
        }else if(item.status.equals("1")){
            holder.mStatusLL.setVisibility(View.VISIBLE);
            holder.mStatusTV.setText("待审核");
            holder.mStatusTV.setTextColor(Color.RED);
        }else if(item.status.equals("2")){
            holder.mStatusLL.setVisibility(View.VISIBLE);
            holder.mStatusTV.setText("审核失败");
            holder.mStatusTV.setTextColor(Color.RED);
        }
        return view;
    }


    class ViewHolder{
        /**
         * 商家头像
         */
        ImageView mImg;

        /**
         * 商家名称
         */
        TextView mName;

        /**
         * 商家入驻时间
         */
        TextView mTime;

        /**
         * 商家地址
         */
        TextView mAddress;

        /**
         * 商家电话
         */
        TextView mTelPhone;

        /**
         * 商家审核状态栏 如果正常则不显示该栏 如果 待审核 or 审核失败 则显示该栏
         *
         */

        LinearLayout mStatusLL;

        /**
         * 商家审核状态显示 待审核 or 审核失败 红字
         */
        TextView mStatusTV;



    }


    //刷新数据
    public void refreshData(List<ShangjiaList> list, boolean refreshOrLoad) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

}
